package com.example.demo.controller;

import com.example.demo.entity.SceneryOrder;
import com.example.demo.entity.User;
import com.example.demo.service.SceneryOrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;


@RestController
@RequestMapping("/sceneryOrder")
public class SceneryOrderController {
    private SceneryOrderService sceneryOrderService;

    @Autowired
    public void setSceneryOrderService(SceneryOrderService sceneryOrderService) {
        this.sceneryOrderService = sceneryOrderService;
    }
    @GetMapping("/page")
    public PageInfo<SceneryOrder> page(@RequestParam("pageNo") Integer pageNo, @RequestParam(defaultValue = "3") int pageSize){
        System.out.println(pageNo+"=========");
        PageInfo<SceneryOrder> pageInfo = sceneryOrderService.getPage(pageNo,pageSize);
        return pageInfo ;
    }
    @GetMapping("/add")
    public String add(SceneryOrder sceneryOrder, HttpSession session){
        Object o=session.getAttribute("user");
        System.out.println(session.getId());
        User us=(User) o;
        System.out.println(us+"=========");
        //if(o==null){
         //   return "nologin";
      //  }else{

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") ;


            sceneryOrder.setUserId(us.getUid());
        System.out.println(us.getUid());
            System.out.println(sceneryOrder.getTime()+"======");
      //  Date date = new Date(sceneryOrder.getTime()) ;
            //sceneryOrder.setTime(simpleDateFormat.format(date));
            int r=sceneryOrderService.add(sceneryOrder);
            System.out.println(r);
            if(r>0){
                return "ok";
            }else{
                return "error";
            }
       // }
    }

    //获取当前用户的列表
    @GetMapping("/getscenerorder")
    private String getOrder(HttpSession session){
        Object o=session.getAttribute("user");
        System.out.println(session.getId());
        if(o==null){
            return "未登录";
        }else{
            User u=(User)o;
            List<SceneryOrder> list=sceneryOrderService.getOrderByUserId(u.getUid());
            return "ok";
        }
    }

    @GetMapping("/delete")
    public String delete(int id) {
        int r = sceneryOrderService.delete(id);
        if (r > 0) {
            return "删除成功";
        } else {
            return "no";
        }
    }
}
